var myPasswordFactory = {
    // _this : this,
    encrypt : function(string,password){


        console.log('encrypt开始运行');
        var myName = 'hscHom';
        
        if(string === ''){
            console.log('加密文本不能为空');
            return string;
        }


        if(password.length < 6){
            console.log('密码少于6个字符');
            return '密码不能少于6个字符';
        }

        if(password.length >24){
            console.log('密码多于24个字符');
            return '密码不能多于24个字符';
        }

        console.log('输入数据判断完成，无非法数据');

        return this.stringEncrypt(string,password) +this.passwordEncrypt(myName,password);
        
    },

    decrypt : function(string,password){
        console.log('解密模块开始运行');
        var text = string.substr(0,string.length-6);
        var password1 = string.substr(string.length-6,string.length);

        if(this.checkPassord(password1,password)){
            console.log('密码验证完成，密码正确');
            return this.stringDecrypt(text,password);
        }else{
            console.log('密码错误');
            return '密码错误';
        }
    },

    passwordEncrypt : function(myName,password){
        console.log('开始进行密码加密');
        var pssswordLenght = password.length;
        var rePassword = [];
        var str;
            if(pssswordLenght === myName.length){                
                for(var i=0 ; i<myName.length ; i++){
                    if(i < pssswordLenght){
                        str = String.fromCharCode(myName.charCodeAt(i) + password.charCodeAt(i) +password.charCodeAt(i+1));
                    }else{
                        str = String.fromCharCode(myName.charCodeAt(i) + password.charCodeAt(i) +password.charCodeAt(0));
                    }
                    rePassword.push(str);
                }
            }else if(pssswordLenght > 6 && pssswordLenght <= 12){
                for(var i=0 ; i<myName.length ; i++){
                    if(i < pssswordLenght/2){
                        str = String.fromCharCode(myName.charCodeAt(i) + password.charCodeAt(i) +password.charCodeAt(pssswordLenght -i -1));
                    }else{
                        str = String.fromCharCode(myName.charCodeAt(i) + password.charCodeAt(i) +password.charCodeAt(pssswordLenght/2 +i%2));
                    }
                    rePassword.push(str);
                }  
            }else if(pssswordLenght > 12 && pssswordLenght <= 24){
                for(var i=0 ; i<myName.length ; i++){
                    if(i < 3){
                        str = String.fromCharCode(myName.charCodeAt(i) + password.charCodeAt(i) +password.charCodeAt(pssswordLenght/2 -1));
                    }else{
                        str = String.fromCharCode(myName.charCodeAt(i) + password.charCodeAt(i) +password.charCodeAt(pssswordLenght -i -1));
                    }
                    rePassword.push(str);
                }
            }
                   
            console.log('密码加密完成');
            console.log('加密后的密码：'+rePassword.join(''))
            return rePassword.join('');
    },

    checkPassord : function(password,inputPassword){
        console.log('密码验证开始，内部调用了加密模块');
        var myName = 'hscHom';
        var checkStr=this.passwordEncrypt(myName,inputPassword);
        
        console.log('密码即将判断结束开始');
        if(checkStr === password){
            return true;
        }else{
            return false;
        }
    },

    stringEncrypt : function(string,password){
        console.log('开始进行文本加密');
        var reString = [];
        for(var i=0 ; i<string.length ; ){
            for(var j=0;j < password.length;j++,i++){
                if(i === string.length)
                    break;
                reString.push(String.fromCharCode(password.charCodeAt(j) +string.charCodeAt(i)));
            }
        }
        console.log('文本加密完成');
        return reString.join('');
    },

    stringDecrypt : function(string,password){
        console.log('开始进行文本解密');
        var reString = [];
        for(var i=0 ; i < string.length ; ){
            for(var j=0 ; j < password.length ; j++,i++){
                if(i === string.length)
                    break;
                reString.push(String.fromCharCode(string.charCodeAt(i) -password.charCodeAt(j)));
            }
        }
        console.log('文本解密完成');

        return reString.join('');
    }
}